home *** CD-ROM | disk | FTP | other *** search
/ MacHack 1996 / MacHack 1996.toast / Hacks / Hacks ’95 / Menu Controls / UPictShape.cp < prev    next >
Encoding:
Text File  |  1995-06-24  |  9.3 KB  |  381 lines  |  [TEXT/MPS ]

  1. // Copyright © 1994-95 by Apple Computer, Inc. All rights reserved.
  2. // UShapes.cp
  3.  
  4. #ifndef __UPICTSHAPE__
  5. #include "UPictShape.h"
  6. #endif
  7.  
  8. // MacApp
  9.  
  10. #ifndef __UDIALOG__
  11. #include <UDialog.h>
  12. #endif
  13.  
  14. #ifndef __UFILE__
  15. #include <UFile.h>
  16. #endif
  17.  
  18. #ifndef __UMEMORY__
  19. #include <UMemory.h>
  20. #endif
  21.  
  22. #ifndef __USTREAM__
  23. #include <UStream.h>
  24. #endif
  25.  
  26. #ifndef __UMACAPPUTILITIES__
  27. #include <UMacAppUtilities.h>
  28. #endif
  29.  
  30. #ifndef __UVIEW__
  31. #include <UView.h>
  32. #endif
  33.  
  34. // Toolbox
  35.  
  36. #ifndef __QUICKDRAW__
  37. #include <Quickdraw.h>
  38. #endif
  39.  
  40. #ifndef __FONTS__
  41. #include <Fonts.h>
  42. #endif
  43.  
  44. // DrawShapes
  45.  
  46. #ifndef __PATTERNMENU__
  47. #include "PatternMenu.h"
  48. #endif
  49.  
  50. #ifndef __UDRAWSHAPES__
  51. #include "UDrawShapes.h"
  52. #endif
  53.  
  54. #ifndef __USHAPEVIEW__
  55. #include "UShapeView.h"
  56. #endif
  57.  
  58. //----------------------------------------------------------------------------------------
  59.  
  60. const Boolean kUseOutlineFonts = true;
  61.  
  62. const short kPictureInset = 4;
  63.  
  64. const ResNumber kDefaultPictureID = 1000;
  65.  
  66.  
  67. //========================================================================================
  68. // CLASS TDocPicture
  69. //========================================================================================
  70. #undef Inherited
  71. #define Inherited TPicture
  72.  
  73. #pragma segment DlgOpen
  74. MA_DEFINE_CLASS_M1(TDocPicture, Inherited);
  75.  
  76. //----------------------------------------------------------------------------------------
  77. // TDocPicture constructor
  78. //----------------------------------------------------------------------------------------
  79. #pragma segment DlgOpen
  80.  
  81. TDocPicture::TDocPicture()
  82. {
  83. }
  84.  
  85.  
  86. //----------------------------------------------------------------------------------------
  87. // TDocPicture::ReleasePicture: 
  88. //----------------------------------------------------------------------------------------
  89. #pragma segment DlgNonRes
  90.  
  91. void TDocPicture::ReleasePicture() // override
  92. {
  93.     if ((fRsrcID != kNoResource) && (fDataHandle != NULL))
  94.         HPurge((Handle)fDataHandle);
  95.     
  96.     fRsrcID = kNoResource;
  97.     fDataHandle = NULL;
  98. }
  99.  
  100.  
  101. //========================================================================================
  102. // CLASS TPictShape
  103. //========================================================================================
  104. #undef Inherited
  105. #define Inherited TShape
  106.  
  107. #pragma segment ARes
  108. MA_DEFINE_CLASS_M1(TPictShape, Inherited);
  109.  
  110. //----------------------------------------------------------------------------------------
  111. // TPictShape Constructor
  112. //----------------------------------------------------------------------------------------
  113. #pragma segment AOpen
  114.  
  115. TPictShape::TPictShape()
  116. {
  117.     fPicture = NULL;
  118.     fPictData = NULL;
  119. }
  120.  
  121. //----------------------------------------------------------------------------------------
  122. // TPictShape::IPictShape
  123. //----------------------------------------------------------------------------------------
  124. #pragma segment AOpen
  125.  
  126. void TPictShape::IPictShape(const CRect& itsExtent, short itsID)
  127. {
  128.     IShape(itsExtent, itsID);
  129. }
  130.  
  131. //----------------------------------------------------------------------------------------
  132. // TPictShape::DoInitialState
  133. //----------------------------------------------------------------------------------------
  134. #pragma segment AOpen
  135.  
  136. void TPictShape::DoInitialState(TShapeView* itsView)
  137. {
  138.     Inherited::DoInitialState(itsView);
  139.     
  140.     CreatePicture(itsView);
  141. }
  142.  
  143. //----------------------------------------------------------------------------------------
  144. // TPictShape::CreatePicture
  145. //----------------------------------------------------------------------------------------
  146. #pragma segment AOpen
  147.  
  148. void TPictShape::CreatePicture(TView* itsView)
  149. {
  150.     if (!fPicture)
  151.     {
  152.         VPoint loc(fLocation);
  153.         VPoint size(fSize);
  154.         VPoint inset(kPictureInset, kPictureInset);
  155.         loc += inset;
  156.         size -= inset;
  157.         size -= inset;
  158.         
  159.         fPicture = new TDocPicture;
  160.         fPicture->IPicture(itsView, loc, size, sizeFixed, sizeFixed, kDefaultPictureID);
  161.     }
  162. }
  163.  
  164. //----------------------------------------------------------------------------------------
  165. // TPictShape::SetPictureFrame
  166. //----------------------------------------------------------------------------------------
  167. #pragma segment AOpen
  168.  
  169. void TPictShape::SetPictureFrame()
  170. {
  171.     if (fPicture)
  172.     {
  173.         VPoint loc(fLocation);
  174.         VPoint size(fSize);
  175.         VPoint inset(kPictureInset, kPictureInset);
  176.         loc += inset;
  177.         size -= inset;
  178.         size -= inset;
  179.         VRect newFrame(loc, loc + size);
  180.         fPicture->SetFrame(newFrame, kInvalidate);
  181.     }
  182. }
  183.  
  184. //----------------------------------------------------------------------------------------
  185. // TPictShape::Clone
  186. //----------------------------------------------------------------------------------------
  187. #pragma segment AClose
  188.  
  189. TObject* TPictShape::Clone()    // Override
  190. {
  191.     TPictShape* bozo = (TPictShape*) Inherited::Clone();
  192.     FailNIL(bozo);
  193.     
  194.     bozo->fPicture = NULL;
  195.     bozo->fPictData = NULL;
  196.     
  197.     if (fPictData)
  198.     {
  199.         Size pictSize = GetHandleSize((Handle) fPictData);
  200.         PicHandle pictData = (PicHandle) NewPermHandle(pictSize);
  201.         FailNIL(pictData);
  202.         MABlockMove(*fPictData, *pictData, pictSize);
  203.         bozo->fPictData = pictData;
  204.     }
  205.     
  206.     return bozo;
  207. }
  208.  
  209. //----------------------------------------------------------------------------------------
  210. // TPictShape::Free
  211. //----------------------------------------------------------------------------------------
  212. #pragma segment AClose
  213.  
  214. void TPictShape::Free()    // Override
  215. {
  216.     fPicture = NULL; // (TDocPicture*) FreeIfObject(fPicture);
  217.     fPictData = NULL; // (PicHandle) DisposeIfHandle((Handle) fPictData);
  218.  
  219.     Inherited::Free();
  220. }
  221.  
  222. //----------------------------------------------------------------------------------------
  223. // TPictShape::ReadFrom
  224. //----------------------------------------------------------------------------------------
  225. #pragma segment AReadFile
  226.  
  227. void TPictShape::ReadFrom(TStream* aStream)    // Override
  228. {
  229.     Inherited::ReadFrom(aStream);
  230.     
  231.     PicHandle pictData = (PicHandle) aStream->ReadHandle();
  232.     SetPicture(pictData);
  233. }
  234.  
  235. //----------------------------------------------------------------------------------------
  236. // TPictShape::WriteTo
  237. //----------------------------------------------------------------------------------------
  238. #pragma segment AWriteFile
  239.  
  240. void TPictShape::WriteTo(TStream* aStream)    // Override
  241. {
  242.     Inherited::WriteTo(aStream);
  243.     
  244.     aStream->WriteHandle((Handle) fPictData);
  245. }
  246.  
  247. //----------------------------------------------------------------------------------------
  248. // TPictShape::SetPicture
  249. //----------------------------------------------------------------------------------------
  250. #pragma segment AClipboard
  251.  
  252. void TPictShape::SetPicture(PicHandle pictData)
  253. {
  254.     if (fPictData != pictData)
  255.     {
  256.         fPictData = (PicHandle) DisposeIfHandle((Handle) fPictData);
  257.         fPictData = pictData;
  258.     }
  259.     
  260.     if (!fPicture)
  261.         CreatePicture(NULL);
  262.     
  263.     if (fPicture && (GetHandleSize((Handle) fPictData) > 0))
  264.         fPicture->SetPicture(fPictData, kDontRedraw);
  265. }
  266.  
  267. //----------------------------------------------------------------------------------------
  268. // TPictShape::BeInView
  269. //----------------------------------------------------------------------------------------
  270. #pragma segment ARes
  271.  
  272. void TPictShape::BeInView(TShapeView* itsView)    // Override
  273. {
  274.     //    Inherited::BeInView(itsView);
  275.     
  276.     if (!fPicture)
  277.         CreatePicture(itsView);
  278.     else
  279.         itsView->AddSubView(fPicture);
  280.     
  281.     if (fPictData)
  282.         SetPicture(fPictData);
  283. }
  284.  
  285. //----------------------------------------------------------------------------------------
  286. // TPictShape::Draw
  287. //----------------------------------------------------------------------------------------
  288. #pragma segment ARes
  289.  
  290. void TPictShape::Draw()    // Override
  291. {
  292.     CRect extent;
  293.     GetFrame(extent);
  294.     
  295.     PenNormal();
  296.     if (qNeedsColorQD || gConfiguration.hasColorQD)
  297.     {
  298.         // Get the color of the menu item representing the shape's color
  299.         RGBForeColor(fColor);
  300.         DrawInsides(extent);
  301.         ForeColor(blackColor);
  302.     }
  303.     else
  304.         DrawInsides(extent);
  305.     
  306.     if (!fPicture)
  307.     {
  308.         CStr255 s("\pP");
  309.     
  310.         TextFont(geneva);
  311.         TextFace(normal);
  312.         TextSize(9);
  313.         TextMode(srcOr);
  314.     
  315.         short x = (extent.left + extent.right) / 2;
  316.         short w = StringWidth(s) / 2;
  317.         CRect r(x - w, extent.bottom - 14, x + w, extent.bottom - 4);
  318.         EraseRect(r);
  319.         MADrawString(s, r, teCenter, kUseOutlineFonts);
  320.     }
  321.  
  322.     DrawOutline();
  323. }
  324.  
  325. //----------------------------------------------------------------------------------------
  326. // TPictShape::DrawInsides
  327. //----------------------------------------------------------------------------------------
  328. #pragma segment ARes
  329.  
  330. void TPictShape::DrawInsides(const CRect& extent)
  331. {
  332.     CRect r(extent);
  333.     
  334.     r.bottom = extent.top + kPictureInset;
  335.     FillRect(r, &gPat[fPattern]);
  336.     r.bottom = extent.bottom;
  337.     
  338.     r.right = extent.left + kPictureInset;
  339.     FillRect(r, &gPat[fPattern]);
  340.     r.right = extent.right;
  341.     
  342.     r.left = extent.right - kPictureInset;
  343.     FillRect(r, &gPat[fPattern]);
  344.     r.left = extent.left;
  345.     
  346.     r.top = extent.bottom - kPictureInset;
  347.     FillRect(r, &gPat[fPattern]);
  348.     r.top = extent.top;
  349. }
  350.  
  351. //----------------------------------------------------------------------------------------
  352. // TPictShape::DrawOutline
  353. //----------------------------------------------------------------------------------------
  354. #pragma segment ARes
  355.  
  356. void TPictShape::DrawOutline()
  357. {
  358.     PenSize(1, 1);
  359.     CRect extent;
  360.     GetFrame(extent);
  361.     FrameRect(extent);
  362. }
  363.  
  364. //----------------------------------------------------------------------------------------
  365. // TPictShape::SetFrame
  366. //----------------------------------------------------------------------------------------
  367. #pragma segment AClipboard
  368.  
  369. void TPictShape::SetFrame(const CRect& extentRect) // override
  370. {
  371.     CRect oldFrame;
  372.     GetFrame(oldFrame);
  373.     if (oldFrame != extentRect)
  374.     {
  375.         Inherited::SetFrame(extentRect);
  376.         if (fPicture)
  377.             SetPictureFrame();
  378.     }
  379. }
  380.  
  381.